package com.qf.product.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.qf.device.entity.Device;
import com.qf.device.mapper.DeviceMapper;
import com.qf.dto.PageDto;
import com.qf.dto.ProductDto;
import com.qf.product.vo.PageVo;
import com.qf.product.entity.Product;
import com.qf.product.mapper.ProductMapper;
import com.qf.product.service.impl.ProductServiceImpl;
import com.qf.tenant.entity.Tenant;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author 
 * @since 2023-10-26
 */
@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    /*分页查询*/
    @PostMapping("/page")
    public PageVo<Product> list(@RequestBody PageDto<Product> pageDto) {
        return productService.page(pageDto);
    }

    //产品添加
    @PostMapping("/add")
    public Result add(@RequestBody ProductDto productDto){
        Product product = new Product();
        product.setName(productDto.getName());
        product.setAreaCode(productDto.getAreaCode().toString()
                .replace(",","-")
                .replace("[","")
                .replace("]","")
                .replace(" ","")
        );
        final long id = IdWorker.getId();
        Tenant tenant = (Tenant) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        product.setId(id);
        product.setTenantId(tenant.getId());
        String ProductKey="product_"+product.getAreaCode().toString().split("-")[0]+"_"+product.getTenantId();
        product.setProductKey(ProductKey);
        product.setUsername(ProductKey);
        product.setPassword("e10adc3949ba59abbe56e057f20f883e");
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        product.setDeleted(0);
        productService.save(product);
        return new Result(0,"success");
    }

    /*查看产品详细信息*/
    @GetMapping("/detail/{productId}")
    public List<Product> detail(@PathVariable Long productId){
        return productMapper.findByProductId(productId);
    }

    /*产品删除*/
    @GetMapping("/delete/{productId}")
    public Result delete(@PathVariable Long productId){
        final List<Device> deviceList = deviceMapper.findByProductId(productId);
        int sum=0;
        for (Device device : deviceList) {
            if (device.getDeleted()==0){
                return new Result(1,"删除失败，请先删除当前产品所关联的设备！");
            }
            if (device.getDeleted()==1){
                //计算产品关联telent中删除的数量
                sum++;
            }
        }
        //判断关联的设备是否与sum相等
        if (deviceList.size()==sum){
            productMapper.deleteById(productId);
            return new Result(0,"删除成功");
        }
        return null;
    }
}
